<?php

class Utils_Image
{	
	private $fileSeparator;
	private $resizeDirectory;
	private $extensionsJpeg = array ("jpeg", "jpg");
	
	public function __construct($fileSeparator='/', $resizeDirectory='/pix/') {
		$this->fileSeparator = $fileSeparator;
		$this->resizeDirectory = $resizeDirectory;
	}
	
	private function extensionIsJpeg($extension)
	{
		foreach ($this->extensionsJpeg as $extensionJpeg) {
			if (strcasecmp($extension, $extensionJpeg) == 0) {
				return true;
			}
		}
		return false;
	}
	
	private function imageIsJpeg($pathImage)
	{
		$extension = pathinfo($pathImage, PATHINFO_EXTENSION);
		return $this->extensionIsJpeg($extension);
	}
	
	private function renameInJpeg($pathImage)
	{
		$extension = pathinfo($pathImage, PATHINFO_EXTENSION);
		return str_replace('.'.$extension, '.jpeg', $pathImage);
	}
	
	public function getResizedPath($pathImage, $width, $height) {
		$files = explode($this->fileSeparator, $pathImage);
		$nameImage = array_pop($files);
		return $this->resizeDirectory.$this->fileSeparator.$width.'x'.$height.'_'.$nameImage;
	}
	
	public function resize($pathImage, $widthResize, $heightResize) {

		// Lit les dimensions de l'image
		$size = GetImageSize($pathImage);
		$src_w = $size[0];
		$src_h = $size[1];
		$src_type = $size[2];
			
		// Teste les dimensions tenant dans la zone
		$test_h = round(($widthResize / $src_w) * $src_h);
		$test_w = round(($heightResize / $src_h) * $src_w);
		
		// Préserve les proportions
		if ($test_h <= $heightResize) {
			$heightResize = $test_h;
		}
		else {	
			$widthResize = $test_w;
		}

        $resizePathImage = $this->getResizedPath($pathImage, $widthResize, $heightResize);

		// La vignette existe ?
		$test = (file_exists($resizePathImage));

		// L'original a été modifié ?
		if ($test) {
			$test = (filemtime($resizePathImage)>filemtime($pathImage));
		}

		// Les dimensions de la vignette sont correctes ?
		if ($test) {
			$size2 = GetImageSize($resizePathImage);
			$test = ($size2[0]==$widthResize);
			$test = ($size2[1]==$heightResize);
		}

		// Créer la vignette ?
		if (!$test) {
			// Crée une image vierge aux bonnes dimensions
			$dst_im = ImageCreateTrueColor($widthResize,$heightResize);
			
			// Chargement de l'image initiale en fonction de son type (png,gif ou jpg)
			switch ($src_type) {
				case "1":
    				$src_im = ImageCreateFromGif($pathImage);
    				break;
				case "2":
   					$src_im = ImageCreateFromJpeg($pathImage);
    				break;
				case "3":
    				$src_im = ImageCreateFromPng($pathImage);
    				break;
    			default:
    				return null;
			}
			
			ImageCopyResampled($dst_im,$src_im,0,0,0,0,$widthResize,$heightResize,$src_w,$src_h);
			// Sauve la nouvelle image
			ImageJpeg($dst_im,$resizePathImage);
			// Détruis les tampons
			ImageDestroy($dst_im);
			ImageDestroy($src_im);
		}
		$size[0]+=0;
		$size[1]+=0;
		$size[2]=$widthResize;
		$size[3]=$heightResize;
		
		return $resizePathImage;
	}
}